<template>
  <el-dialog :visible.sync="value" :before-close="() => $emit('input', false)">
    <template slot="title">
      {{isCreated ? '新增' : '编辑'}}三方平台应用
    </template>
    <el-form :model="form" ref="refForm" :rules="rules" label-width="180px">
      <el-form-item
        label="支付渠道"
        prop="limitType"
        placeholder="请选择支付渠道"
      >
        <el-radio-group
          v-model="form.limitType"
          :disabled="!isCreated"
          @change="onChangeLimitType"
        >
          <el-radio
            v-for="item in payEnum.PAY_TYPE"
            :label="item.value"
            :key="item.value"
            v-show="item.value !== -1"
          >
            {{item.title}}
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="支付类型" prop="paymentId">
        <el-select
          v-model="form.paymentId"
          placeholder="请选择支付类型"
          :disabled="!isCreated"
        >
          <el-option
            v-for="item in paymentManageList"
            :key="item.paymentId"
            :value="item.paymentId"
            :label="item.paymentName"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="三方平台" prop="channelId">
        <el-select
          v-model="form.channelId"
          placeholder="请选择三方平台"
          @change="onChangeEscrowPlatformChange"
          :disabled="!isCreated"
          filterable
        >
          <el-option
            v-for="o in escrowPlatformList"
            :label="o.channelName"
            :value="o.channelId"
            :key="o.channelId"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="商户号" prop="merchantId">
        <el-select
          v-model="form.merchantId"
          placeholder="请选择商户号"
          :disabled="!isCreated"
        >
          <el-option
            v-for="o in shopNoList"
            :label="o.merchantCode"
            :value="o.merchantId"
            :key="o.merchantId"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="应用端" prop="clientType">
        <el-select
          v-model="form.clientType"
          placeholder="请选择应用端"
        >
          <el-option
            v-for="o in payEnum.DEVICE_TYPE"
            :label="o.title"
            :value="o.value"
            :key="o.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="层级" prop="levelId">
        <el-select
          multiple
          collapse-tags
          v-model="form.levelId"
          placeholder="请选择层级类型"
        >
          <el-option
            v-for="item in userLevelList"
            :label="item.name"
            :value="item.levelId"
            :key="item.levelId"
          ></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="备注" prop="remark">
        <el-col :span="16">
          <el-input type="textarea" v-model="form.remark"></el-input>
        </el-col>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button
        @click="$emit('input', false)"
      >
        取 消
      </el-button>
      <el-button
        type="primary"
        @click="doSave({action: 'saveForm', form: 'refForm'})"
      >
        确 定
      </el-button>
    </div>
  </el-dialog>
</template>

<script>

// services
import {
  getPaymentManageList,
  getEscrowShopNoList,
} from '@/services/api/recharge';
import {
  getDepositAppAccountInuse,
  updateDepositApplication,
} from '@/services/api/payment';
import {
  getUserLevelList,
} from '@/services/api/userLevel';

// mixins
import { form, factory } from '@/build';

export default {
  name: 'edit-deposit-app-dialog',
  props: {
    rowData: {
      type: Object,
      default: () => ({
        itemList: [],
      }),
    },
    value: {
      type: Boolean,
      default: true,
    },
    id: {
      type: Number,
      default: undefined,
    },
    type: {
      type: String,
      default: '',
    },
  },
  mixins: [form, factory],
  data() {
    return {
      firstEnter: false,
      shopNoList: [],
      paymentManageList: [],
      escrowPlatformList: [],
      userLevelList: [],
      form: {
        id: this.id,
        limitType: 0,
        paymentId: null,
        channelId: null,
        merchantId: null,
        clientType: null,
        levelId: [],
        remark: null,
        ...this.rowData,
      },
      applyData: {},
      rules: {
        paymentId: [
          { required: true, message: '支付类型不能为空' },
        ],
        channelId: [
          { required: true, message: '三方平台不能为空' },
        ],
        merchantId: [
          { required: true, message: '商户号不能为空' },
        ],
        clientType: [
          { required: true, message: '应用端为不能为空' },
        ],
        levelId: [
          { required: true, message: '层级不能为空' },
        ],
        remark: [
          { required: true, message: '备注不能为空' },
        ],
      },
    };
  },
  watch: {
    'form.paymentId': {
      handler() {
        if (!this.firstEnter) {
          this.firstEnter = true;
          return;
        }
        this.form.channelId = undefined;
        this.form.merchantId = undefined;
      },
      deep: true,
      immediate: true,
    },
  },
  computed: {
    isCreated() {
      return !(this.rowData && this.rowData.id);
    },
  },
  methods: {
    initPropsToState() {
      if (!(this.rowData && Object.keys(this.rowData))) return;
      const { levelId = '' } = this.rowData;
      const list = levelId ? levelId.split(',') : [];
      this.form.levelId = list.map(item => Number(item));
    },
    onChangeLimitType() {
      this.form.channelId = null;
      this.form.merchantId = null;
      this.escrowPlatformList = [];
      this.shopNoList = [];
      this.doGet({ action: 'getEscrowPlatformList' });
    },
    async onChangeEscrowPlatformChange(val) {
      this.form.merchantId = null;
      this.shopNoList = [];
      try {
        const [bool, result] = await getEscrowShopNoList({ channelId: val, limitType: this.form.limitType });
        if (!bool) return;
        this.shopNoList = result.list || [];
      } catch (e) {
        // don't handle
      }
    },
  },
  created() {
    this.initPropsToState();
  },
  mounted() {
    this.bindSave(updateDepositApplication, 'saveForm', 'form', {
      beforeSend: (data) => {
        const { levelId } = data;
        return {
          ...data,
          id: this.id,
          levelId: (Array.isArray(levelId) && levelId.length) ? levelId.join(',') : '',
        };
      },
      afterSuccess: () => {
        this.$emit('input', false);
        this.$parent.gridSearch();
      },
    });

    if (this.id) {
      this.bindGet(getEscrowShopNoList, 'shopNoList', {
        sendingData: { channelId: this.form.channelId },
        format: data => data.list || [],
      });
    }

    this.bindGet(getPaymentManageList, 'paymentManageList');
    this.bindGet(getDepositAppAccountInuse, 'escrowPlatformList', {
      action: 'getEscrowPlatformList',
      beforeSend: () => ({
        limitType: this.form.limitType,
      }),
    });
    this.bindGet(getUserLevelList, 'userLevelList');
    this.getAll();
  },
};
</script>

<style scoped>
</style>
